package org.cx.web;

import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.cx.dao.UserDetailMapper;
import org.cx.model.UserDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value="/user")
public class UserDetailController {

	private static Log log = LogFactory.getLog(UserDetailController.class);
		
	@Autowired
	private UserDetailMapper userDetailMapper;
	
	@RequestMapping(value="/queryById",method=RequestMethod.GET)
	public String query() {
		UserDetail ud = userDetailMapper.selectByPrimaryKey(15);
		log.info(ud.toString());
		
		return "/index.jsp";
	}

	@RequestMapping(value="/queryAll",method=RequestMethod.GET)
	@ResponseBody
	public List<UserDetail> queryAll() {
		UserDetail param = new UserDetail();
		param.setNickName("面包");
		param.setEmail("aaa@wqe.com");
		List<UserDetail> list = userDetailMapper.selectByParam(param);
		
		for (UserDetail ud : list) {
			log.info(ud.toString());
		}
		
		return list;
	}
	
	@RequestMapping(value="/queryByEmail",method=RequestMethod.GET)
	@ResponseBody
	public List<UserDetail> queryByEmail() {
		List<UserDetail> list = userDetailMapper.selectByEmail("aa@163.com");
		
		for (UserDetail ud : list) {
			log.info(ud.toString());
		}
		
		return list;
	}
	
	@RequestMapping(value="/insert")
	@ResponseBody
	public int insert() {
		UserDetail ud = new UserDetail();
		ud.setNickName("面包");
		ud.setEmail("aaa@wqe.com");
		ud.setIntro("面包好吃不");
		ud.setPasswd("111111");
		ud.setSex(1);
		ud.setCreateTime(new Date());
		ud.setUpdateTime(new Date());
		userDetailMapper.insert(ud);
		
		return 0;
	}
	
	@RequestMapping(value="/updateById")
	@ResponseBody
	public int updateById() {
		UserDetail ud = new UserDetail();
		ud.setId(32);
		ud.setNickName("水果");
		ud.setEmail("321@reqe.com");
		ud.setIntro("一点点奶茶");
		ud.setPasswd("4324");
		ud.setSex(1);
		ud.setUpdateTime(new Date());
		int row = userDetailMapper.updateById(ud);
		
		return row;
	}
}
